
let user = null;


let API_DOMAIN = '';


let isCompleteBind = false;
let isCompleteQa = false;

function getUser() {
    $.post('./api/get_user.php', {
        openid: $.cookie('openid')
    }).done(function (res) {
        console.log(res);
        if (res.code === 0) {
            user = res.data;
            isCompleteBind = true;
            $('#shop_score').text(user.shop_score);
            $('#my_score').text(user.score);
            $('#full_name').text(user.full_name);
            $('#mobile').text(user.mobile);
            $('#shop').text(user.shop);
            $('#completeInfo').hide();
            $('.person-show').show();
            return
        }
    });
}

getUser();

$.post('./api/post_qa.php', {
    openid: $.cookie('openid'),
    week: $('input[name=week]').val(),
    score: -1
}).done(function (res) {
    console.log(res);
    if (res.code === -3) {

    }
    if (res.code === -2) {
        isCompleteQa = true;
        let myScore = res.data.score;
        $('#qa-week-score').text(myScore);
        $('.qa-completed').fadeIn();
        $('#qa-form').fadeOut();

    }

});


function getRankings() {
    $.post('./api/get_ranking.php', {}).done(function (res) {
        if (res.code === 0) {
            let rangkingsList = res.data;

            let rankingsHtml = ' <tr>\
                                    <th>排名</th>\
                                    <th>名称</th>\
                                    <th>积分</th>\
                                 </tr>';

            $.each(rangkingsList, function (i) {
                let NO = i + 1;
                rankingsHtml += '<tr>\
                                    <td>' + NO + '</td>\
                                    <td>' + this['shop'] + '</td>\
                                    <td>' + this['shop_score'] + '</td>\
                                </tr>';
            });
            $('#rankings').html(rankingsHtml);
        }
    });
}

//
// getRankings();

let mySwiper = new Swiper('.app', {
    effect: 'fade'
});

$('#auto_shop').autocomplete({
    lookup: shopinfo,
    onSelect: function (suggestion) {
        // alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
    }
});

$('.btn-rule').click(function () {
    //自定义标题风格
    layer.open({
        title: [
            '活动规则',
            'background-color: #FF4351; color:#fff; margin: 0;'
        ]
        , content: '活动规则内容。'
    });

});

$('.btn-person').click(function () {
    mySwiper.slideTo(4)
});

$('.btn-checkIn').click(function () {
    if (false === isCompleteBind) {
        showComInfoConfirm(mySwiper);
        return
    }
    getUser();
    mySwiper.slideTo(1)
});

$('.btn-QA').click(function () {
    if (false === isCompleteBind) {
        showComInfoConfirm(mySwiper);
        return
    }
    mySwiper.slideTo(2)
});

$('.btn-Rankings').click(function () {
    if (false === isCompleteBind) {
        showComInfoConfirm(mySwiper);
        return
    }
    getRankings();
    mySwiper.slideTo(3)
});

$('.btn-back').click(function () {
    mySwiper.slideTo(0)
});

// 每日签到
$('.btn-daily').click(function () {
    $.post('./api/check_in.php', {
        openid: $.cookie('openid'),
        shop: user.shop
    }).done(function (res) {
        console.log(res);
        if (res.code === 0) {
            showMsg('签到成功！+6');
            getUser()

        }
        if (res.code === -2) {
            showMsg('您已经签到过！')
        }
    });

});

$('#qa-form').submit(function (e) {
    e.preventDefault();
    let myScore = 0;
    let qaFormArr = $('input[type=radio]:checked');
    let week = $('input[name=week]').val();

    $.each($('input[type=radio]:checked'), function () {

        myScore += parseInt(this.value)
    });
    if (qaFormArr.length < 10) {
        alert('已答' + qaFormArr.length + '题，未答' + (10 - qaFormArr.length) + '题');
        return;
    }
    alert('分数：' + myScore);
    $.post('./api/post_qa.php', {
        openid: $.cookie('openid'),
        week: week,
        score: myScore,
        shop: user.shop
    }).done(function (res) {
        console.log(res);
        if (res.code === 0) {
            $('#qa-week-score').text(myScore);
            $('.qa-completed').fadeIn();
            $('#qa-form').fadeOut();
            isCompleteQa = true;

        }
    });

});

$('#completeInfo').submit(function (e) {
    e.preventDefault();
    let infoArr = $('#completeInfo').serializeArray();
    let infoObj = $('#completeInfo').serializeFormJSON();


    if (!infoObj.full_name) {
        showMsg('姓名不能空！');
        return;
    }
    if (!infoObj.mobile) {
        showMsg('手机号不能空！');
        return;
    }
    if (!isMobile(infoObj.mobile)) {
        showMsg('手机号格式不正确！');
        return;
    }
    if (!infoObj.shop) {
        showMsg('门店信息不能空！');
        return;
    }

    $.post('./api/post_user.php', {
        openid: $.cookie('openid'),
        full_name: infoObj.full_name,
        mobile: infoObj.mobile,
        shop: infoObj.shop
    }).done(function (res) {
        if (res.code === 0) {

            isCompleteBind = true;
            showMsg('感谢完善信息！ +100');
            $('#completeInfo').hide();
            $('.person-show').show();

            isCompleteBind = true;
            mySwiper.slideTo(0);
            return;
        }
        if (res.code === -3) {
            showMsg('手机号已存在！');
            return;
        }
    });


})


function isMobile(mobile) {
    let pattern = /^1[34578]\d{9}$/;
    if (pattern.test(mobile)) {
        return true
    }
    return false
}

function showMsg(text) {
    //提示
    layer.open({
        content: text || '请传入提示信息！'
        , skin: 'msg'
        , time: 2 //2秒后自动关闭
    });
}

function showComInfoConfirm(mySwiper) {

    //询问框
    layer.open({
        content: '你没完善个人信息，完善后加100分'
        , btn: ['好的', '不要']
        , yes: function (index) {
            // location.reload();
            mySwiper.slideTo(4);
            layer.close(index);
        }
    });
}

